home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************
- *
- * GLUT API implementation for TinyGL (AmigaDE)
- * (c) 2002 Ruben Monteiro
- *
- * Please see the file license.txt included in the archive for license details
- *
- ****************************************************************/
-
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- #include <lib/time.h>
- #include <elate/ave/ave.h>
- #include <ad709/tinygl/glut.h>
- #include <ad709/tinygl/igl.h>
-
-
- //////////// Variables and typedefs
-
- #define LMB 1
- #define RMB 2
- #define MMB 4
- #define ERROR 0
- #define WARNING 1
- #define __isMouseEvent(a) a >= EV_MOUSEEVENTS && a < EV_MOUSEEVENTS+6
- #define __isKeyEvent(a) a >= EV_KEYBOARDEVENTS && a < EV_KEYBOARDEVENTS+3
- #define __getMB(a) a == 0 ? __glut_currMBDown : (a == LMB ? GLUT_LEFT_BUTTON : (a == RMB ? GLUT_RIGHT_BUTTON : GLUT_MIDDLE_BUTTON))
- #define __getMBState(a) a == EV_BUTTONDOWN ? GLUT_DOWN : GLUT_UP
- #define __getMEntry(a) a == EV_ENTER ? GLUT_ENTERED : GLUT_LEFT
-
-
- typedef struct {
- ave_dev_t *ave;
- ave_app_t *app;
- ave_toolkit_t *tkit;
- char *appname;
- ave_win_t *win;
- char execname[255];
- } glut_ave_t;
-
-
- typedef struct {
- int x, y, width, height;
- ave_win_t *win;
- GLboolean redisplay;
- GLboolean reshape;
- GLboolean reposition;
- } glut_window_t;
-
-
-
- static glut_ave_t __glut_ave;
- static int __glut_initX = 0, __glut_initY = 0;
- static int __glut_initWidth = 240, __glut_initHeight = 320;
- static int __glut_mouseX = 0, __glut_mouseY = 0;
- static int __glut_currMBDown = -1;
- static glut_window_t __glut_window;
- static IGLContext __glut_ctx = NULL;
- static int __glut_currWindow = 0;
- static int __glut_numWindows = 0;
- static long __glut_initTime;
- static GLboolean __glut_fullScreen = GL_FALSE;
- static GLboolean __glut_windowCreated = GL_FALSE;
- static char* __glut_windowName;
-
-
- typedef void (*glut_displayFunc_t) ();
- typedef void (*glut_reshapeFunc_t) (int width, int height);
-
- typedef void (*glut_idleFunc_t) ();
- typedef void (*glut_keyboardFunc_t) (unsigned char key, int x, int y);
- typedef void (*glut_mouseFunc_t) (int button, int state, int x, int y);
- typedef void (*glut_motionFunc_t) (int x, int y);
- typedef void (*glut_passiveMotionFunc_t) (int x, int y);
- typedef void (*glut_entryFunc_t) (int state);
-
-
- static glut_displayFunc_t __glut_displayFunc = NULL;
- static glut_idleFunc_t __glut_idleFunc = NULL;
- static glut_keyboardFunc_t __glut_keyboardFunc = NULL;
- static glut_keyboardFunc_t __glut_keyboardUpFunc = NULL;
- static glut_mouseFunc_t __glut_mouseFunc = NULL;
- static glut_motionFunc_t __glut_motionFunc = NULL;
- static glut_passiveMotionFunc_t __glut_passiveMotionFunc = NULL;
- static glut_reshapeFunc_t __glut_reshapeFunc = NULL;
- static glut_entryFunc_t __glut_entryFunc = NULL;
- static ave_screen_info_t __glut_screen;
-
-
-
- //////////// Auxiliary functions
-
-
- void __glutMessage(int type, const char *format, ...) {
- va_list args;
- va_start(args, format);
- if (type == WARNING) {
- fprintf(stderr, "GLUT: Warning in %s: ", __glut_ave.execname);
- }
- else {
- fprintf(stderr, "GLUT: Fatal error in %s: ", __glut_ave.execname);
- }
- vfprintf(stderr, format, args);
- va_end(args);
- }
-
-
- void __glutWarning(const char *format, ...) {
- __glutMessage(WARNING, format);
- }
-
-
- void __glutError(const char *format, ...) {
- __glutMessage(ERROR, format);
- exit(0);
- }
-
-
- void __shutdownGLUT() {
- glutDestroyWindow(0);
- }
-
- void __setupWindow() {
- if (__glut_displayFunc == NULL) {
- __glutError("redisplay needed for window, but no display callback.\n");
- }
- if (__glut_fullScreen) {
- __glut_window.width = __glut_initWidth = glutGet(GLUT_SCREEN_WIDTH);
- __glut_window.height = __glut_initHeight = glutGet(GLUT_SCREEN_HEIGHT);
- __glut_window.x = __glut_window.y = 0;
- __glut_window.win = ave_toolkit_createdialog(__glut_ave.tkit, __glut_windowName, NULL,
- __glut_window.width, __glut_window.height, FDI_CONTENT|FDI_FILLED);
- }
- else {
- __glut_window.win = ave_toolkit_createdialog(__glut_ave.tkit, __glut_windowName, NULL,
- __glut_window.width, __glut_window.height,
- FDI_BORDER|FDI_TITLE|FDI_CONTENT|FDI_DRAG|FDI_FILLED|FDI_CLOSE);
- ave_avo_addlink((ave_avo_t *) __glut_window.win, (ave_evt_t *) __glut_ave.app, CH_DIALOG_ACTION, EV_QUIT);
- }
- ave_app_addavo(__glut_ave.app, NULL, (ave_avo_t *) __glut_window.win, 0);
- ave_avo_setpos((ave_avo_t *) __glut_window.win, __glut_window.x, __glut_window.y, 0);
- ave_avo_settoken((ave_avo_t *) __glut_window.win, BFAVO_KEYTOKEN);
- iglMakeCurrent(__glut_window.win, __glut_ctx);
- __glut_window.width = ave_avo_getsize((ave_avo_t *) __glut_window.win).width;
- __glut_window.height = ave_avo_getsize((ave_avo_t *) __glut_window.win).height;
- __glut_windowCreated = GL_TRUE;
- }
-
-
- GLboolean __windowResized() {
- return (ave_avo_getsize((ave_avo_t *) __glut_window.win).width != __glut_window.width
- || ave_avo_getsize((ave_avo_t *) __glut_window.win).height != __glut_window.height);
- }
-
-
-
- //////////// GLUT API Functions
-
-
- void glutInit(int *argcp, char **argv) {
- strcpy(__glut_ave.execname, argv[0]);
- if (__glut_ctx != NULL) {
- __glutWarning("glutInit being called a second time.\n");
- return;
- }
- __glut_initTime = microtime(NULL);
- __glut_ctx = iglCreateContext();
- kn_dev_lookup("/device/ave", (ELATE_OO_DATA **) &__glut_ave.ave, &__glut_ave.appname);
- __glut_ave.app = ave_dev_open(__glut_ave.ave, (ELATE_OO_DATA *) __glut_ave.appname, 0, 0);
- __glut_ave.tkit = ave_dev_opentoolkit(__glut_ave.ave, __glut_ave.app);
- }
-
-
- void glutInitWindowPosition(int x, int y) {
- __glut_window.x = __glut_initX = x;
- __glut_window.y = __glut_initY = y;
- }
-
-
- void glutInitWindowSize(int width, int height) {
- __glut_window.width = __glut_initWidth = width;
- __glut_window.height = __glut_initHeight = height;
- }
-
-
- int glutCreateWindow(char *name) {
- __glut_windowName = name;
- __glut_window.redisplay = GL_TRUE;
- __glut_window.reshape = GL_TRUE;
- __glut_window.reposition = GL_FALSE;
- return __glut_numWindows++;
- }
-
-
- void glutSwapBuffers() {}
-
-
- void glutFullScreen(void) {
- __glut_fullScreen = GL_TRUE;
- __setupWindow();
- }
-
-
- void glutSetWindow(int win) {
- __glut_currWindow = win;
- }
-
-
- int glutGetWindow(void) {
- return __glut_currWindow;
- }
-
-
- void glutDestroyWindow(int win) {
- iglDestroyContext(__glut_ctx);
- ave_app_closeall(__glut_ave.app);
- ave_dev_closetoolkit(__glut_ave.ave, __glut_ave.tkit);
- ave_dev_close(__glut_ave.ave, __glut_ave.app);
- __glut_numWindows--;
- }
-
-
-
- void glutMainLoop(void) {
- ave_event_t ave_event;
- ave_message_t *msg;
- ave_avo_pos_t windowPos;
-
- if (!__glut_windowCreated) {
- __setupWindow();
- }
- do {
- ave_event = ave_app_getevent(__glut_ave.app, 0);
- msg = ave_event.msg;
- if (ave_event.target != NULL) {
- ave_evt_event(ave_event.target, ave_event.msg, ave_event.type);
-
- // ************** Handle mouse Events
- if (__isMouseEvent(msg->EVD_TYPE)) {
- __glut_mouseX = msg->EVD_RX;
- __glut_mouseY = msg->EVD_RY;
- if (msg->EVD_TYPE == EV_BUTTONDOWN || msg->EVD_TYPE == EV_BUTTONUP) {
- if (msg->EVD_TYPE == EV_BUTTONDOWN) {
- ave_avo_settoken((ave_avo_t *) __glut_window.win, BFAVO_KEYTOKEN);
- ave_avo_tofront((ave_avo_t *) __glut_window.win);
- __glut_currMBDown = __getMB(msg->EVD_BUTTONS);
- }
- if (__glut_mouseFunc != NULL) {
- __glut_mouseFunc(__getMB(msg->EVD_BUTTONS), __getMBState(msg->EVD_TYPE),
- __glut_mouseX, __glut_mouseY);
- }
- if (msg->EVD_TYPE == EV_BUTTONUP) {
- __glut_currMBDown = -1;
- }
- }
- if (msg->EVD_TYPE == EV_TRACKING) {
- if (__glut_currMBDown == -1 && __glut_passiveMotionFunc != NULL) {
- __glut_passiveMotionFunc(__glut_mouseX, __glut_mouseY);
- }
- if (__glut_currMBDown != -1 && __glut_motionFunc != NULL) {
- __glut_motionFunc(__glut_mouseX, __glut_mouseY);
- }
- }
- if (msg->EVD_TYPE == EV_ENTER || msg->EVD_TYPE == EV_LEAVE) {
- if (__glut_entryFunc != NULL) {
- __glut_entryFunc(__getMEntry(msg->EVD_TYPE));
- }
- }
- }
-
- // ************** Handle keyboard events
- if (__isKeyEvent(msg->EVD_TYPE)) {
- if (msg->EVD_TYPE == EV_KEYDOWN && __glut_keyboardFunc != NULL) {
- __glut_keyboardFunc(msg->EVD_KEY, __glut_mouseX, __glut_mouseY);
- }
- if (msg->EVD_TYPE == EV_KEYUP && __glut_keyboardUpFunc != NULL) {
- __glut_keyboardUpFunc(msg->EVD_KEY, __glut_mouseX, __glut_mouseY);
- }
- }
- }
-
- ave_dev_freeevent((ave_dev_t *) __glut_ave.ave, msg);
-
- // ************* Handle callbacks
- windowPos = ave_avo_getpos((ave_avo_t *) __glut_window.win);
- __glut_window.x = windowPos.x;
- __glut_window.y = windowPos.y;
- if (__glut_window.reposition) {
- ave_avo_setpos((ave_avo_t *) __glut_window.win, __glut_window.x, __glut_window.y, 0);
- __glut_window.reposition = GL_TRUE;
- }
- if (__glut_window.redisplay) {
- __glut_displayFunc();
- ave_avo_update((ave_avo_t *) __glut_window.win);
- __glut_window.redisplay = GL_FALSE;
- }
- if ((__glut_window.reshape || __windowResized()) && __glut_reshapeFunc != NULL) {
- if (__windowResized() && !__glut_window.reshape) {
- __glut_window.width = ave_avo_getsize((ave_avo_t *) __glut_window.win).width;
- __glut_window.height = ave_avo_getsize((ave_avo_t *) __glut_window.win).height;
- }
- ave_avo_change((ave_avo_t *) __glut_window.win, __glut_window.x, __glut_window.y,
- __glut_window.width, __glut_window.height, CM_ADD);
- __glut_reshapeFunc(__glut_window.width, __glut_window.height);
- iglResizeContext(__glut_ctx, __glut_window.width, __glut_window.height, __glut_window.win);
- __glut_window.reshape = GL_FALSE;
- }
- if (__glut_idleFunc != NULL) {
- __glut_idleFunc();
- }
- } while (ave_event.type != EV_QUIT);
- __shutdownGLUT();
- }
-
-
-
- void glutPostRedisplay(void) {
- __glut_window.redisplay = GL_TRUE;
- }
-
-
- void glutDisplayFunc(void (*func)(void)) {
- __glut_displayFunc = func;
- }
-
-
- void glutReshapeFunc(void (*func)(int width, int height)) {
- __glut_reshapeFunc = func;
- }
-
-
- void glutIdleFunc(void (*func)(void)) {
- __glut_idleFunc = func;
- }
-
-
-
- void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)) {
- __glut_keyboardFunc = func;
- }
-
-
- void glutKeyboardUpFunc(void (*func)(unsigned char key, int x, int y)) {
- __glut_keyboardUpFunc = func;
- }
-
-
- void glutMouseFunc(void (*func)(int button, int state, int x, int y)) {
- __glut_mouseFunc = func;
- }
-
-
- void glutMotionFunc(void (*func)(int x, int y)) {
- __glut_motionFunc = func;
- }
-
-
- void glutPassiveMotionFunc(void (*func)(int x, int Y)) {
- __glut_passiveMotionFunc = func;
- }
-
-
- void glutEntryFunc(void (*func)(int state)) {
- __glut_entryFunc = func;
- }
-
-
- void glutShowWindow(void) {
- ave_avo_show((ave_avo_t *) __glut_window.win);
- }
-
-
- void glutHideWindow(void) {
- ave_avo_hide((ave_avo_t *) __glut_window.win);
- }
-
-
- void glutPositionWindow(int x, int y) {
- __glut_window.x = x;
- __glut_window.y = y;
- __glut_window.reposition = GL_TRUE;
- }
-
-
- void glutReshapeWindow(int width, int height) {
- __glut_window.width = width;
- __glut_window.height = height;
- __glut_window.reshape = GL_TRUE;
- }
-
-
- int glutGet(GLenum state) {
- __glut_screen = ave_avo_getscreen((ave_avo_t *) __glut_ave.ave);
- switch (state) {
- case GLUT_WINDOW_X :
- return __glut_window.x;
- case GLUT_WINDOW_Y :
- return __glut_window.y;
- case GLUT_WINDOW_WIDTH :
- return __glut_window.width;
- case GLUT_WINDOW_HEIGHT :
- return __glut_window.height;
- case GLUT_WINDOW_DEPTH_SIZE :
- return 16;
- case GLUT_WINDOW_PARENT :
- return 0;
- case GLUT_WINDOW_NUM_CHILDREN :
- return 0;
- case GLUT_SCREEN_WIDTH :
- return __glut_screen.width;
- case GLUT_SCREEN_HEIGHT :
- return __glut_screen.height;
- case GLUT_SCREEN_WIDTH_MM :
- return 0;
- case GLUT_SCREEN_HEIGHT_MM :
- return 0;
- case GLUT_INIT_WINDOW_X :
- return __glut_initX;
- case GLUT_INIT_WINDOW_Y :
- return __glut_initY;
- case GLUT_INIT_WINDOW_WIDTH :
- return __glut_initWidth;
- case GLUT_INIT_WINDOW_HEIGHT :
- return __glut_initHeight;
- case GLUT_ELAPSED_TIME :
- return (int) ((microtime(NULL) - __glut_initTime)/1000);
- }
- return -1;
- }
-
-
- ///// Not implemented
- void glutInitDisplayMode(unsigned int mode) {}
-